package cn.edu.hstc.wm.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import cn.edu.hstc.wm.adapter.GuidePagerAdapter;
import cn.edu.hstc.wm.utils.LogUtil;

import cn.edu.hstc.wm.R;

/**
 * @ClassName: GuideActivity
 * @Description: 引导页的界面
 * 
 * @ModificationHistory
 */
public class AppGuide extends Activity implements OnPageChangeListener, OnClickListener {
	private static final String TAG = AppGuide.class.getSimpleName();

	/** ViewPager控件 */
	private ViewPager mViewPager;
	private GuidePagerAdapter mAdapter;
	/** 显示内容页 */
	private List<View> listViews;

	/** 小圆点图片数组 */
	private ImageView[] dots;

	/** 当前所在页面的索引位置 */
	private int currentIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guide_layout);

		LogUtil.i(TAG, "onCreate");

		// 初始化
		initViews();

		// 初始化小圆点
		initDots();
	}

	private void initViews() {
		LayoutInflater inflater = LayoutInflater.from(this);

		listViews = new ArrayList<View>();

		// 从布局文件载入View作为适配器数据
		listViews.add(inflater.inflate(R.layout.what_new_0, null));
		listViews.add(inflater.inflate(R.layout.what_new_1, null));
		listViews.add(inflater.inflate(R.layout.what_new_2, null));
		listViews.add(inflater.inflate(R.layout.what_new_3, null));

		mViewPager = (ViewPager) findViewById(R.id.viewpager);

		mViewPager.setPageMargin(20);
		mAdapter = new GuidePagerAdapter(listViews, this);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(this);
	}

	private void initDots() {
		LinearLayout ll = (LinearLayout) findViewById(R.id.ll_guide_dot);

		dots = new ImageView[listViews.size()];

		//
		for (int i = 0; i < listViews.size(); i++) {
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);// 设置为未选中状态,即灰色
			dots[i].setOnClickListener(this); // 监听小圆点点击事件
		}

		currentIndex = 0;
		dots[currentIndex].setEnabled(false);// ̬指定当前的位置为选中状态
	}

	// 设置小圆点
	private void setCurrentDot(int position) {
		// 不能越界
		if (position < 0 || position > listViews.size() - 1 || currentIndex == position) {
			return;
		}

		dots[position].setEnabled(false); // 当前选中了
		dots[currentIndex].setEnabled(true); // 上一个为未选中状态

		currentIndex = position; // 替换为当前选中位置
	}

	// 滚动状态改变
	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	// 页面滑动
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	// 选中当前页面
	@Override
	public void onPageSelected(int arg0) {
		setCurrentDot(arg0);
	}

	@Override
	public void onClick(View v) {
		// 监听到点击了哪个小圆点,然后就跳转到该页面
		int goIndex = 0;
		switch (v.getId()) {
		case R.id.img_dot_0:
			goIndex = 0;
			break;
		case R.id.img_dot_1:
			goIndex = 1;
			break;
		case R.id.img_dot_2:
			goIndex = 2;
			break;
		case R.id.img_dot_3:
			goIndex = 3;
			break;
		default:
			break;
		}
		setCurrentDot(goIndex);
		mViewPager.setCurrentItem(goIndex, true); // 选中指定位置的页面
	}

}